package cn.neusoft.controller;

import cn.neusoft.domain.CargoDescription;
import cn.neusoft.domain.CargoDistributeBill;
import cn.neusoft.domain.CargoExImportBill;
import cn.neusoft.domain.Warehouse;
import cn.neusoft.service.CargoAndDescriptionService;
import cn.neusoft.service.ExImportBillService;
import cn.neusoft.tool.PageSet;
import cn.neusoft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/exImportBill")
public class ExImportController {

    @Autowired
    private ExImportBillService exImportBillService;
    @Autowired
    private CargoAndDescriptionService cargoAndDescriptionService;

    //    @PostMapping("/ExImportQueryAll")
//    public ResultBean ExImportBillQueryAll(Map<Object, Object> map){
//        ResultBean resultBean = null;
//
//        if(map != null){
//            if(map.containsKey("warehouse") && map.containsKey("from") && map.containsKey("to") && map.containsKey("to")){
//                Warehouse warehouse = (Warehouse) map.get("warehouse");
//                Date from = (Date) map.get("from");
//                Date to = (Date) map.get("to");
//                int type = (int) map.get("to");
//                if(warehouse.getCargoWamLevel() == null){
//                    warehouse.setCargoWamLevel(new HashMap<String, Double>());
//                }
//                if(warehouse.getCargoMaxLevel() == null){
//                    warehouse.setCargoMaxLevel(new HashMap<String, Double>());
//                }
//                if(warehouse.getCargos() == null){
//                    warehouse.setCargos(new HashMap<String, Double>());
//                }
//                List<CargoExImportBill> list = exImportBillService.ExImportBillQueryAll(warehouse.getName(), from, to);
//                resultBean = ResultBean.success(list);
//            }
//
//        }
//        else{
//            resultBean = ResultBean.error(7000, "输入为空");
//        }
//
//        return resultBean;
//    }
    @PostMapping("/getAllExImportPage")

    public ResultBean getAllExImportPage(@RequestBody Map<String, String> map) {
        ResultBean resultBean;

        List<CargoExImBillReturn> ans = new ArrayList<CargoExImBillReturn>();


        try {
            int pageNum = Integer.parseInt(map.get("pageNum"));
            int pageSize = Integer.parseInt(map.get("pageSize"));
            Date from = null;
            Date to = null;
            if (!map.get("from").equals("")) {
                from = new SimpleDateFormat("yyyy-MM-dd").parse(map.get("from"));
            }
            if (!map.get("to").equals("")) {
                to = new SimpleDateFormat("yyyy-MM-dd").parse(map.get("to"));
            }
            String warehouseName = map.get("warehouseName");
            String cargoName = map.get("cargoName");
            String type = map.get("type");

            List<CargoExImportBill> list = exImportBillService.ExImportBillQueryAll(warehouseName, from, to);

            for (CargoExImportBill c : list) {
                boolean isHad = false;
                if (!type.equals("") && c.getType() != Integer.parseInt(type)) {
                    continue;
                }
                List<ExImTableElement> line = new ArrayList<>();
                double quantities = 0;
                double totalPrice = 0;
                for (Map.Entry<String, Double> entry : c.getCargosInf().entrySet()) {
                    if (cargoName.equals("") || entry.getKey().equals(cargoName)) {
                        isHad = true;
                    }
                    CargoDescription cargoDescription = cargoAndDescriptionService.getByDesName(entry.getKey());
                    ExImTableElement exImTableElement = null;
                    if (cargoDescription != null) {
                        exImTableElement = new ExImTableElement(entry.getKey(), entry.getValue(), Double.toString(cargoDescription.getSalePrice()),
                                Integer.toString(cargoDescription.getId()), cargoDescription.getProvider());
                        totalPrice += cargoDescription.getSalePrice() * entry.getValue();
                    } else {
                        exImTableElement = new ExImTableElement(entry.getKey(), entry.getValue(), "未设置商品",
                                "未设置商品", "未设置商品");
                    }
                    line.add(exImTableElement);
                    quantities += entry.getValue();

                }
                if (isHad) {
                    CargoExImBillReturn cargoExImBillReturn = new CargoExImBillReturn(c.getId(), c.getWarehouseName(), c.getMissionID(), c.getDate(), c.getType(), quantities, Double.toString(totalPrice), line);
                    ans.add(cargoExImBillReturn);
                }
            }


            PageSet<CargoExImBillReturn> pageSet = new PageSet<CargoExImBillReturn>(ans, pageSize);
            Map<String, Object> answer = new HashMap<>();
            answer.put("total", pageSet.getTotal());
            answer.put("list", pageSet.getPage(pageNum));

            resultBean = ResultBean.success(answer);

        } catch (Exception e) {
            e.printStackTrace();
            resultBean = ResultBean.error(1, "错误");
        }

        return resultBean;
    }

    private class ExImTableElement {
        private String cargoName;
        private double quantities;
        private String price;
        private String id;
        private String provider;

        public ExImTableElement(String cargoName, double quantities, String price, String id, String provider) {
            this.cargoName = cargoName;
            this.quantities = quantities;
            this.price = price;
            this.id = id;
            this.provider = provider;
        }

        public String getCargoName() {
            return cargoName;
        }

        public void setCargoName(String cargoName) {
            this.cargoName = cargoName;
        }

        public double getQuantities() {
            return quantities;
        }

        public void setQuantities(double quantities) {
            this.quantities = quantities;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getProvider() {
            return provider;
        }

        public void setProvider(String provider) {
            this.provider = provider;
        }
    }

    private class CargoExImBillReturn {
        private int id;
        private String warehouseName;
        private int missionID;
        private java.sql.Date date;
        private int stage;
        private double quantities;
        private String totalPrice;
        private List<ExImTableElement> list;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getWarehouseName() {
            return warehouseName;
        }

        public void setWarehouseName(String warehouseName) {
            this.warehouseName = warehouseName;
        }

        public int getMissionID() {
            return missionID;
        }

        public void setMissionID(int missionID) {
            this.missionID = missionID;
        }

        public java.sql.Date getDate() {
            return date;
        }

        public void setDate(java.sql.Date date) {
            this.date = date;
        }

        public int getStage() {
            return stage;
        }

        public void setStage(int stage) {
            this.stage = stage;
        }

        public double getQuantities() {
            return quantities;
        }

        public void setQuantities(double quantities) {
            this.quantities = quantities;
        }

        public String getTotalPrice() {
            return totalPrice;
        }

        public void setTotalPrice(String totalPrice) {
            this.totalPrice = totalPrice;
        }

        public List<ExImTableElement> getList() {
            return list;
        }

        public void setList(List<ExImTableElement> list) {
            this.list = list;
        }

        public CargoExImBillReturn(int id, String warehouseName, int missionID, java.sql.Date date, int stage, double quantities, String totalPrice, List<ExImTableElement> list) {
            this.id = id;
            this.warehouseName = warehouseName;
            this.missionID = missionID;
            this.date = date;
            this.stage = stage;
            this.quantities = quantities;
            this.totalPrice = totalPrice;
            this.list = list;
        }
    }
}
